import csv

import requests
import re

url = 'https://www.9ku.com/music/t_new.htm'

res =requests.get(url)

html = res.text
'<a target="_1" href="/play/1387274.htm" class="songName ">可可托海的牧羊人 </a>'
title = re.findall(r'<a target="_1" href="(.*)" class="songName ">(.*)</a>', html)
lst = []
for x,y in title:
    dic = {}
    dic['歌曲名'] = y
    dic['链接'] = 'https://www.9ku.com'+x
    lst.append(dic)

head = ('歌曲名', '链接')

with open('九酷网歌曲.csv', 'w', encoding='utf-8-sig',newline='') as f:
    w = csv.DictWriter(f,fieldnames=head)
    w.writeheader()
    w.writerows(lst)
